from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings


def generate_access_token(openid):
    """
    签名openid
    :param openid:用户的openid
    :return: access_token
    """
    serializer = Serializer(settings.SECRET_KEY,300)

    data = {'openid':openid}

    token = serializer.dumps(data)
    return token.decode()

def check_access_token(access_token):
    """校验签名"""
    serializer = Serializer(settings.SECRET_KEY,300)
    try:
        data = serializer.loads(access_token)
    except BadData:
        return None
    else:
        return data['openid']
